/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:18:21
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.risk.service.impl;

import com.joysuch.wwyt.bp.entity.BpTaskList;
import com.joysuch.wwyt.bp.repository.BpTaskListDao;
import com.joysuch.wwyt.bp.service.BpTaskListService;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.RiskDistrictEvaluation;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.joysuch.wwyt.risk.repository.RiskDistrictEvaluationDao;
import com.joysuch.wwyt.risk.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RiskDistrictEvaluationServiceImpl implements RiskDistrictEvaluationService {

    @Autowired
    private RiskDistrictEvaluationDao dao;
    @Autowired
    private RiskDistrictDao riskDistrctDao;
    @Autowired
    private RiskDistrictService riskDistrictService;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BpTaskListService taskListService;
    @Autowired
    private BpTaskListDao taskListDao;
    @Autowired
    private RiskEvaluationAlarmMesageService evaluationAlarmMessageService;
    @Autowired
    private RiskEvaluationRepoService riskEvaluationRepoService;
    @Autowired
    private RiskEvaluationPlanService riskEvaluationPlanService;

    @Override
    public void add(RiskDistrictEvaluation entity) throws Exception {
        // entity.setCode(bizCodeGeneratorFacade.fe);
        Long riskDistrictId = entity.getRiskDistrictId();
        RiskDistrict district = riskDistrctDao.findById(entity.getRiskDistrictId()).get();
        entity.setCode(district.getCode());
        //entity.setEvaluationUserId(Context.getCurrentUserId());
        entity.setEvaluationUserId(entity.getEvaluationUserId());
        //Date evaluationTime = new Date();
        Date evaluationTime = entity.getEvaluationTime();
        entity.setEvaluationTime(evaluationTime);
        dao.save(entity);
        //更新评估信息
        district.setRiskDistrictEvaluation(entity);
        district.setLastEvaluationYear(Calendar.getInstance().get(Calendar.YEAR));
        district.setEvaluationState(1);
        riskDistrctDao.save(district);
        //每次评估重新计算风险评估计划下次评估时间
        riskEvaluationPlanService.districtReCal(district, evaluationTime,null);
        //更新风险等级
        riskDistrictService.updateDistrictRiskLevel(riskDistrictId);
        List<BpTaskList> bpTaskList = taskListService.findById(district.getId());
        if (bpTaskList != null) {
            for (BpTaskList task : bpTaskList) {
                task.setState(2);
                task.setDetail("区域风险评估");
                task.setHandleRemark("区域风险评估完成");
                // 处理当前时间
                task.setHandleTime(new Date());
                task.setHandleUserId(Context.getCurrentUserId());
            }
            taskListDao.saveAll(bpTaskList);
        }
    }

    @Override
    public void update(RiskDistrictEvaluation entity) throws Exception {
        RiskDistrictEvaluation content = dao.findById(entity.getId()).get();
        if (content != null) {
            // TODO set nessessary properties
            dao.save(entity);
            //跟新风险等级
            riskDistrictService.updateDistrictRiskLevel(content.getRiskDistrictId());
        }
    }

    @Override
    public Page<RiskDistrictEvaluation> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public RiskDistrictEvaluation findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public RiskDistrictEvaluation findLastestOfDistrict(Long riskDistrictId) {
        return dao.findFirstByRiskDistrictIdOrderByCreateTimeDesc(riskDistrictId);
    }

}
